package com.genius.workflow.exception;

import com.genius.reservation.common.ResponseRule;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.json.MappingJackson2JsonView;
import com.genius.adminmanager.other.entity.ResponseJson;
import javax.servlet.http.HttpServletRequest;

@ControllerAdvice
public class GlobalDefaultExceptionHandler {

    @ExceptionHandler(value = Exception.class)
    public ModelAndView  defaultErrorHandler(HttpServletRequest request, Exception e) throws Exception {
    	
        
        boolean isNowPower = true;
        Object o = request.getAttribute("method_return_is_view");
        if( o == null) {
            throw e;
        }
        boolean isView = (Boolean)o;
        if(isView){  //返回页面
            if(e instanceof org.apache.shiro.authz.UnauthorizedException){
            	isNowPower = false;
            	return new ModelAndView("/views/error/nopower");
            }
            if(e instanceof org.apache.shiro.authz.AuthorizationException){
            	isNowPower = false;
            	return new ModelAndView("/views/error/nopower");
            }
        }
        ModelAndView mv = new ModelAndView(new MappingJackson2JsonView());
        mv.addObject(ResponseJson.code, 0);
        if(e instanceof org.apache.shiro.authz.UnauthorizedException){
        	mv.addObject(ResponseJson.status_No_Power, "您没用操作权限");
        	isNowPower = false;
        }
        if(e instanceof org.apache.shiro.authz.AuthorizationException){
        	mv.addObject(ResponseJson.status_No_Power, "您没用操作权限");
        	isNowPower = false;
        }
//        if(e instanceof  java.lang.NullPointerException){
//            return ResponseRule.no("服务器错误");
//        }
        if(isNowPower){
        	//打印异常信息：
            e.printStackTrace();
        }
		return mv;
    }
}

